package com.cloud.wms.ui.sort.presenter;


import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.http.error.ApiException;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.entity.Page;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.group.entity.BatchCommodityEntity;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.sort.contract.BatchContract;
import com.cloud.wms.ui.sort.entity.BatchDetailEntity;
import com.cloud.wms.ui.sort.entity.BatchInfoEntity;

import java.util.ArrayList;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * @Author:Chen
 * @Date: 2021-01-14 15:51
 * @Description:
 */
public class BatchPresenter extends BaseRxPresenter<BatchContract.View> implements BatchContract.Presenter {
    public static final int BATCH_DETAIL = 0x104;

    @Inject
    public BatchPresenter() {

    }

    @Override
    public void loadBatchDetail(TaskEntity taskEntity) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("BatchCode", taskEntity.getMainBillCode());

        Observable<BatchInfoEntity> info = FineExApplication
                .component()
                .http()
                .sortBatchList(params)
                .map(new ResponseWrapper<>())
                .map(Page::getItems)
                .flatMap((Function<ArrayList<BatchInfoEntity>, ObservableSource<BatchInfoEntity>>) batchInfoEntities -> {
                    if (batchInfoEntities == null || batchInfoEntities.isEmpty()) {
                        return Observable.error(new ApiException("波次信息获取失败！"));
                    }
                    return Observable.just(batchInfoEntities.get(batchInfoEntities.size() - 1));
                });

        Params paramsDetail = Params.newInstance(Params.Type.MEMBER);
        paramsDetail.put("BatchID", taskEntity.getMainBillID());

        Observable<ArrayList<CommodityEntity>> detail = FineExApplication
                .component()
                .http()
                .sortTaskBatchDetail(paramsDetail)
                .map(new ResponseWrapper<>());

        Observable.zip(info, detail, (batchInfoEntity, commodityEntities) -> {
            BatchDetailEntity detailEntity = new BatchDetailEntity();
            detailEntity.setCommodityEntities(commodityEntities);
            detailEntity.setInfoEntity(batchInfoEntity);
            return detailEntity;
        })
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<BatchDetailEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(BatchDetailEntity info) {
                        mView.onSuccess(MessageCreator.createMessage(info, BATCH_DETAIL));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                        mView.showError();
                    }
                });
    }
}
